package com.timofeyco.springsocialwithscribedemo.web.controllers;

import com.timofeyco.springsocialwithscribedemo.social.facebook.impl.FacebookOAuthManager;
import com.timofeyco.springsocialwithscribedemo.social.twitter.impl.TwitterOAuthManager;
import com.timofeyco.springsocialwithscribedemo.social.vkontakte.UserInfo;
import com.timofeyco.springsocialwithscribedemo.social.vkontakte.impl.VKontakteOAuthManager;
import org.scribe.model.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SocialServicesController {
    @Autowired TwitterOAuthManager twitterOAuthManager;
    @Autowired FacebookOAuthManager facebookOAuthManager;
    @Autowired VKontakteOAuthManager vkontakteOAuthManager;

    @RequestMapping(value = "/twitter/login", method = RequestMethod.GET)
    public ModelAndView loginAuthenticateTW() {
        ModelAndView mv = new ModelAndView("redirect:" + twitterOAuthManager.getAuthorizationURL());
        return mv;
    }

    @RequestMapping(value = "/twitter/after-login", method = RequestMethod.GET)
    public ModelAndView afterLoginTW(
            @RequestParam(value = "denied", required = false) String error,
            @RequestParam(value = "oauth_verifier", required = false) String oauth_verifier) {
        ModelAndView mv = new ModelAndView();
        if (error != null) {
            mv.setViewName("redirect:/");
            return mv;
        }
        Token accessToken = twitterOAuthManager.getOAuthAccessToken(oauth_verifier);
        if (accessToken == null) {
            // something wrong
            mv.setViewName("redirect:/");
            return mv;
        }
        TwitterTemplate template = new TwitterTemplate(twitterOAuthManager.getConsumerKey(), twitterOAuthManager.getConsumerSecret(),
                accessToken.getToken(), accessToken.getSecret());
        TwitterProfile profile = template.userOperations().getUserProfile();

        mv.setViewName("redirect:/profile");
        mv.addObject("service", "Twitter");
        mv.addObject("id", profile.getId());
        mv.addObject("nick", profile.getScreenName());
        mv.addObject("name", profile.getName());
        mv.addObject("photo", profile.getProfileImageUrl());
        return mv;
    }

@RequestMapping(value = "/facebook/login")
    public String loginAuthenticateFB() {
        return this.facebookOAuthManager.loginAuthenticate();
    }

    @RequestMapping(value = "/facebook/after-login", method = RequestMethod.GET)
    public ModelAndView afterLoginFB(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "code", required = false) String code) {
        ModelAndView mv = new ModelAndView();
        if (error != null) {
            mv.setViewName("redirect:/");
            return mv;
        }
        String accessToken = facebookOAuthManager.retrieveAccessToken(code);
        if (accessToken == null) {
            // something wrong
            mv.setViewName("redirect:/");
            return mv;
        }
        FacebookTemplate facebookTemplate = new FacebookTemplate(accessToken);
        FacebookProfile profile = facebookTemplate.userOperations().getUserProfile();

        mv.setViewName("redirect:/profile");
        mv.addObject("service", "Facebook");
        mv.addObject("id", profile.getId());
        mv.addObject("nick", profile.getUsername());
        mv.addObject("name", profile.getName());
        return mv;
    }

    @RequestMapping(value = "/vk/login", method = RequestMethod.GET)
    public ModelAndView loginAuthenticateVK() {
        ModelAndView mv = new ModelAndView(vkontakteOAuthManager.loginAuthenticate());
        return mv;
    }

    @RequestMapping(value = "/vk/after-login", method = RequestMethod.GET)
    public ModelAndView afterLoginVK(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "code", required = false) String code) {
        ModelAndView mv = new ModelAndView();
        if (error != null) {
            mv.setViewName("redirect:/landing");
            return mv;
        }
        String accessToken = vkontakteOAuthManager.retrieveAccessToken(code);
        UserInfo profile = vkontakteOAuthManager.retrieveUserInfoStr(accessToken);
        if (accessToken == null) {
            // something wrong
            mv.setViewName("redirect:/");
            return mv;
        }

        mv.setViewName("redirect:/profile");
        mv.addObject("service", "VKontakte");
        mv.addObject("id", profile.getUser_id());
        mv.addObject("nick", profile.getUser_name());
        mv.addObject("name", profile.getUser_name());
        mv.addObject("photo", profile.getUser_photo());
        return mv;
    }


}
